﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;

namespace WebDataControls
{
    [ToolboxData("<{0}:DataCheckBoxList runat=server></{0}:DataCheckBoxList>")]
    public class DataCheckBoxList : System.Web.UI.WebControls.CheckBoxList, IDataControl
    {
        #region IDataControl 成员

        /// <summary>
        /// 多选时，FeildName 的实际值要用","分割每个选择项
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string FeildName
        {
            get
            {
                String s = (String)ViewState["FeildName"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["FeildName"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableName
        {
            get
            {
                String s = (String)ViewState["TableName"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["TableName"] = value;
            }
        }

        public void GetDataValue(ref object model)
        {
            PropertyInfo prop = model.GetType().GetProperty(this.FeildName, BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite)
            {
                string str_Save = "";
                foreach (ListItem li in this.Items)
                {
                    if (li.Selected == true)
                    {
                        str_Save += li.Value + ",";
                    }
                }
                if (!String.IsNullOrEmpty(str_Save))
                    str_Save = str_Save.Remove(str_Save.Length - 1, 1);
                object value = str_Save;
                Type fieldType = WebUIHelper.GetModelFieldType(model, this.FeildName);
                if (!this.Text.GetType().Equals(fieldType))
                {
                    value = Convert.ChangeType(value, fieldType);
                }
                prop.SetValue(model, value, null);
            }
        }

        public void SetDataValue(object model)
        {
            if (!String.IsNullOrEmpty(this.FeildName))
            {
                object value = model.GetType().GetProperty(this.FeildName, BindingFlags.Public | BindingFlags.Instance).GetValue(model, null);
                if (value == null)
                    return;
                string strValue = value.ToString();
                string[] strtemp = strValue.Split(',');
                foreach (string str in strtemp)
                {
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        if (this.Items[i].Value == str)
                        {
                            this.Items[i].Selected = true;
                        }
                    }
                }
            }
        }

        #endregion

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}
